package com.fengye.algorithms.leecode;

/**
 * @Author fengye
 * @Date 2023/4/8 9:42
 **/
public class IntReverse07 {
    public int reverse(int x) {
        if(x==0) {
            return 0;
        }

        String str = String.valueOf(x);
        boolean f = false;
        if(str.charAt(0) == '-') {
            f  = true;
            str = str.substring(1);
        }
        char[] arr = str.toCharArray();
        StringBuilder sb = new StringBuilder();
        if(f) {
            sb.append('-');
        }
        boolean notZero = false;
        for (int i=arr.length-1;i>=0;i--) {
            char c = arr[i];
            if(c == '0' && !notZero) {
                continue;
            }

            sb.append(c);
            notZero = true;
        }

        long l = Long.parseLong(sb.toString());

        return (int) l == l ? (int)l : 0;
    }
}
